const Db = require('../services/mysql')
const { Fb, Moment, Op, Dev } = require('../class/utils')
const _ = require('lodash')

class Message {
  constructor() {}

  /**
   * 新增留言
   * @param {*} where
   */
  create(form) {
    return Db.Message.create({
      title: form.title,
      content: form.content,
      savetime: Moment.current(),
      tname: form.tname
    })
  }

  /**
   * 查找留言列表
   * @param {*} where
   */
  select(form, nowPage, pageSize) {
    let content = form.content || ''
    return Db.Message.findAndCountAll({
      where: {
        content: {
          [Op.like]: '%' + content + '%'
        }
      },
      order: [['savetime', 'DESC']],
      offset: pageSize * (nowPage - 1),
      limit: pageSize
    })
  }

  /**
   * 留言详情
   * @param {*} where
   */
  details(id) {
    return Db.Message.findById(id)
  }

  /**
   * 回复留言信息
   * @param {*} where
   */
  update(form) {
    return Db.Message.update(
      {
        reply: form.reply,
      },
      {
        where: {
          id: form.id
        }
      }
    )
  }

  /**
   * 删除留言
   * @param {*} where
   */
  delete(from) {
    return Db.Message.destroy({
      where: {
        id: from.id
      }
    })
  }
}

module.exports = new Message()
